﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AD.Web.Common;
using HZT.Framework.Utility;
using AD.Contract;
using AD.Contract.Models;
using HZT.Framework.Contract;
using System.IO;

namespace AD.Web.Areas.ADArea.Controllers
{
    public class ADBillController : ADController
    {
        //
        // GET: /ADArea/ADBill/

        public ActionResult Index(ADBillRequest request)
        {
            //获取部门列表
            var deptList = this.ADService.GetDepartmentList();

            string ss = request.ADBill.ContractId;
            //
            var result = this.ADService.GetADBill(request);
            return View(result);
        }
        //获取客户列表
        public ActionResult SelectCustomer(CommonRequest request)
        {
            var result = this.ADService.GetCustomerList(request);
            return View(result);
        }
        // 获取播出时段 
        public ActionResult SelectPeriod(CommonRequest request)
        {
            var result = this.ADService.GetPeriodList(request);
            return View(result);
        }
        public ActionResult NewBill()
        {
            var model = new AD.Contract.Models.ADBill();
            //这里可以给model设置默认值
            this.RenderMyViewData(model);
            return View(model);
        }
        [HttpPost]  //FormCollection collection
        public ActionResult NewBill(ADBill model)
        {
            //  var model = new ADBill();
            // this.TryUpdateModel(model);
            model.BillType = EBillType.Common.ToString();
            model.Status = EADBillStatus.WaitInfoCheck.ToString();
            model.customer = null;
            try
            {
                this.ADService.SaveADBill(model);
            }
            catch (BusinessException e)
            {
                this.ModelState.AddModelError(e.Name, e.Message);
                this.RenderMyViewData(model);
                return View("NewBill", model);
            }

            return this.RefreshParent();
        }
        #region 特殊广告业务

        public ActionResult SpecialBill()
        {
            var model = new AD.Contract.Models.ADBill();
            //这里可以给model设置默认值
            this.RenderMyViewData(model);
            return View(model);
        }
        [HttpPost]
        public ActionResult SpecialBill(ADBill model)
        {
            model.BillType = EBillType.Special.ToString();
            model.Status = EADBillStatus.WaitInfoCheck.ToString();
            model.customer = null;
            try
            {
                this.ADService.SaveADBill(model);
            }
            catch (BusinessException e)
            {
                this.ModelState.AddModelError(e.Name, e.Message);
                this.RenderMyViewData(model);
                return View("SpecialBill", model);
            }

            return this.RefreshParent();
        }
        #endregion


        public void RenderMyViewData(ADBill model)
        {   //获取部门列表
            var deptList = this.ADService.GetDepartmentList();
            ViewData.Add("DepartmentId", new SelectList(deptList, "Id", "Name", model.DepartmentId));
            //广告业务类型
            var businessTypeList = this.ADService.GetBaseCategory(ECategoryType.BusinessType.ToString());
            ViewData.Add("BusinessCategoryId", new SelectList(businessTypeList, "Id", "Name", model.BusinessCategoryId));
            ////广告类型
            //var adTypeList = this.ADService.GetBaseCategory(ECategoryType.ADType.ToString());
            //ViewData.Add("DepartmentId", new SelectList(adTypeList, "Id", "Name", model.DepartmentId));
            //付款方式
            var payCategoryIdList = this.ADService.GetBaseCategory(ECategoryType.PayMode.ToString());
            ViewData.Add("PayCategoryId", new SelectList(payCategoryIdList, "Id", "Name", model.PayCategoryId));
        }

        #region 信息确认 文件上传
        //获取新录入的广告订单，进行信息确认、文件上传
        public ActionResult InfoCheck(ADBillRequest request)
        {
            //获取部门列表
            var deptList = this.ADService.GetDepartmentList();
            ViewData.Add("DepartmentId", new SelectList(deptList, "Id", "Name", request.ADBill.DepartmentId));
            request.ADBill.Status = EADBillStatus.WaitInfoCheck.ToString();

            var result = this.ADService.GetADBill(request);
            return View(result);
        }
        //更改状态
        public ActionResult ChangeStatus(string Id, string status, string callBack)
        {
            try
            {
                ADBill model = this.ADService.GetADBillById(Id);
                model.Status = status;
                this.ADService.SaveADBill(model);
            }
            catch (BusinessException e)
            {
                this.ModelState.AddModelError(e.Name, e.Message);
                // return View("SpecialBill", model);
            }
            return RedirectToAction(callBack);


        }

        #endregion
        //获取价格确认
        public ActionResult MoneyConfirm(ADBillRequest request)
        {
            var deptList = this.ADService.GetDepartmentList();
            ViewData.Add("DepartmentId", new SelectList(deptList, "Id", "Name", request.ADBill.DepartmentId));

            request.ADBill.Status = EADBillStatus.WaitMoneyConfirm.ToString();
            var result = this.ADService.GetADBill(request);
            return View(result);
        }
        //获取价格确认
        public ActionResult Pass(ADBillRequest request)
        {
            //
            var deptList = this.ADService.GetDepartmentList();
            ViewData.Add("DepartmentId", new SelectList(deptList, "Id", "Name", request.ADBill.DepartmentId));
            request.ADBill.Status = EADBillStatus.WaitPass.ToString();
            var result = this.ADService.GetADBill(request);
            return View(result);
        }
        //播出确认
        public ActionResult PlayConfirm(ADBillRequest request)
        {
            //
            var deptList = this.ADService.GetDepartmentList();
            ViewData.Add("DepartmentId", new SelectList(deptList, "Id", "Name", request.ADBill.DepartmentId));

            request.ADBill.Status = EADBillStatus.WaitPlayConfirm.ToString();
            var result = this.ADService.GetADBill(request);
            return View(result);
        }

        #region  文件上传类
        //上传
        public ActionResult UpLoad(string Id, string cId)
        {
            ViewData.Add("cId", cId);
            var model = new ADAppendix();
            model.ADBillId = Id;
            return View();
        }
        [HttpPost]
        public ActionResult UpLoad(string Id, HttpPostedFileBase Filedata)
        {
            // 没有文件上传，直接返回
            if (Filedata == null || string.IsNullOrEmpty(Filedata.FileName) || Filedata.ContentLength == 0)
            {
                return HttpNotFound();
            }

            //获取文件完整文件名(包含绝对路径)
            //文件存放路径格式：/files/upload/{日期}/{md5}.{后缀名}
            //例如：/files/upload/20130913/43CA215D947F8C1F1DDFCED383C4D706.jpg
            string fileGuidId = CommonFunction.GetNewID();
            string FileEextension = Path.GetExtension(Filedata.FileName);
            string uploadDate = DateTime.Now.ToString("yyyyMMdd");

            string imgType = Request["imgType"];
            string virtualPath = "/";
            if (imgType == "normal")
            {
                virtualPath = string.Format("~/Upload/{0}/{1}{2}", uploadDate, fileGuidId, FileEextension);
            }
            else
            {
                virtualPath = string.Format("~/Upload/{0}/{1}{2}", uploadDate, fileGuidId, FileEextension);
            }
            string fullFileName = this.Server.MapPath(virtualPath);

            //创建文件夹，保存文件
            string path = Path.GetDirectoryName(fullFileName);
            Directory.CreateDirectory(path);
            if (!System.IO.File.Exists(fullFileName))
            {
                Filedata.SaveAs(fullFileName);
            }
            //
            ADAppendix model = new ADAppendix();            
            model.ADBillId = Id;
            model.FileFormat = FileEextension;
            model.FileName = fileGuidId;
            model.OriginalName = Path.GetFileName(Filedata.FileName);
            model.SavePath = uploadDate ;//记录相对路径
            model.CreateTime = DateTime.Now;

            bool isSuccess = false;
            string msg ="";
            try
            {
                this.ADService.SaveADAppendix(model);
                isSuccess = true;
            }
            catch(Exception ex)
            {
                msg = ex.Message;
            }
            var data = new { IsSuccess = isSuccess,Message =msg, orgName=model.OriginalName, imgtype = imgType, imgpath = virtualPath.Remove(0, 1) };
            return Json(data, JsonRequestBehavior.AllowGet);


            //ViewData.Add("cId", cId);
            //var model = new ADAppendix();
            //model.ADBillId = Id;
            //return View();
        }
        #endregion

        #region  广告明细

        public ActionResult Detail(string Id)
        {
            return View();
        }

        #endregion



        #region  广告收款
        public ActionResult ADCharge(ADBillRequest request)
        {
            //
            var result = this.ADService.GetADBill(request);
            return View(result);
        }
        //广告收款
        public ActionResult ADChargeEdit(string Id, string ADBillId, string ContractId, string Company)
        {
            //
            var model = new ADCharge();
            model.ADBillId = ADBillId;
            ViewData.Add("ContractId", ContractId);
            //ViewData.Add("Company", Company);
            model.CustomerId = Company;

            return View(model);
        }
        [HttpPost]
        public ActionResult ADChargeEdit(ADCharge model)
        {
            //
            //var model = this.DeptService.GetDept(Id);
            //this.TryUpdateModel<Department>(model);
            try
            {

                this.ADService.SaveADCharge(model);
            }
            catch (BusinessException e)
            {
                this.ModelState.AddModelError(e.Name, e.Message);
                // return View("Edit", model);
            }

            return this.RefreshParent();
        }
        //广告收款
        public ActionResult ADChargeList(string Id)
        {
            //
            var model = new ADCharge();
            model.ADBillId = Id;
            return View(model);
        }

        #endregion
    }
}
